package b1.scenes.home
{
	import b1.controls.basic.*;
	import b1.net.*;
	import b1.scenes.*;
	
	import com.greensock.TweenLite;
	import com.greensock.easing.Expo;
	
	import flash.display.Sprite;
	import flash.events.*;
	import flash.text.*;
	import flash.utils.Timer;
	
	public class Heirloom extends Sprite
	{
		public var originalOwnerPlayerId:int;
		public var originalPlayerName:String;
		public var currentOwnerPlayerId:int;
		public var timer:Timer;
		public var moveFlag:int;
		private var heirloomMessage:Sprite;
		public var heirloomIcon:BasicMc;
		public var level:int;
		
		public function Heirloom()
		{
			var HeirloomIcon:Class = ResourceHelper.getInstance().getClassByName("Heirloom");
			var heirloomIcon:BasicMc = new BasicMc(HeirloomIcon, 10);
			heirloomIcon.height = 80;
			heirloomIcon.scaleX = heirloomIcon.scaleY;
			addChild(heirloomIcon);
			heirloomIcon.addEventListener(MouseEvent.MOUSE_OVER, showMessage);
			heirloomIcon.addEventListener(MouseEvent.MOUSE_OUT, closeMessage);
			originalPlayerName = "";
			if (Math.random() < 0.5)
			{
				moveFlag = 0;
			}
			else 
			{
				moveFlag = 1;
			}
		}
		
		public function startMove():void
		{
			timer = new Timer(150);
			timer.start();
			timer.addEventListener(TimerEvent.TIMER, onTick);
		}
		
		public function stopMove():void
		{
			timer.stop();
			timer.removeEventListener(TimerEvent.TIMER, onTick);
		}
		
		private function onTick(event:Event):void
		{
			var stepLength:int = 2;
			
			if (moveFlag == 0)
			{
				this.x -= stepLength;
				if(this.x < 150)
				{
					this.moveFlag = 1;
				}
			}
			else 
			{
				this.x += stepLength;
				if(this.x > 500)
				{
					this.moveFlag = 0;
				}
			}
		}
		
		private function showMessage(event:Event):void
		{
			this.stopMove();
			var AnimalMsgBgr:Class = ResourceHelper.getInstance().getClassByName("HeirloomMsgBgr");
			heirloomMessage = new AnimalMsgBgr();
			heirloomMessage.x = this.x + this.width + 5;
			heirloomMessage.y = this.y;

			var nameText:TextField = new TextField();
			nameText.selectable = false;
        	nameText.autoSize = TextFieldAutoSize.LEFT;
			nameText.defaultTextFormat = new TextFormat("微软雅黑", 14, 0x000000);
			nameText.text = "传家骨\n等级：" + this.level + "    原属：" + this.originalPlayerName + "\n作案可能夺得传家骨！";
			nameText.x = 45;
			nameText.y = 5;
			heirloomMessage.addChild(nameText);
			SceneContainer.getInstance().currentScene.addChild(heirloomMessage);
		}

		private function closeMessage(event:Event):void
		{
			this.startMove();
			if (heirloomMessage != null && heirloomMessage.parent != null)
			{
				heirloomMessage.parent.removeChild(heirloomMessage);
			}
		}
						
		
	}
}